package main.pmc;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: 葛成瑞
 * @Date: 2025/2/12 11:03
 */
public class Trans {

    @SneakyThrows
    public static void main(String[] args) {

        Db useTaiyi = Db.use();
        String saveTableFrom = "pmcdownhtml";
        String saveTableDwon = "pmcdownziphtml";
        String allDate = "select max(pmid) as pmid  from " + saveTableFrom;
        Entity query = useTaiyi.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
        Integer count = query.getInt("pmid");
        int breakSize = 1000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMess("开始id++" + start + "++结束id++++" + end);
            String paramSql = "select pmid,pmcid,html from " + saveTableFrom + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = useTaiyi.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String pmcid = entity.getStr("pmcid");
                String pmid = entity.getStr("pmid");
                String html = entity.getStr("html");
                if (StringUtil.isNotNull(html)) {
                    byte[] gzip = ZipUtil.gzip(html, "utf-8");
                    Entity ins = new Entity();
                    ins.set("pmid", pmid);
                    ins.set("pmcid", pmcid);
                    ins.set("html", gzip);
                    ins.setTableName(saveTableDwon);
                    try {
                        useTaiyi.insert(ins);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                } else {
                    Entity ins = new Entity();
                    ins.set("pmid", pmid);
                    ins.set("pmcid", pmcid);
                    ins.setTableName(saveTableDwon);
                    try {
                        useTaiyi.insert(ins);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });

        }


    }
}
